import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/config/lf_type_config.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/res/lf_fonts.dart';
import 'package:largefriends/res/lf_text_style.dart';
import 'package:largefriends/res/lf_widget_ext.dart';
import 'package:largefriends/res/strings/lf_strings.dart';
import 'package:largefriends/utils/lf_scroll_behavior.dart';
import 'package:largefriends/widgets/category_widget.dart';
import 'package:largefriends/widgets/lf_app_bar.dart';
import 'package:largefriends/widgets/lf_button.dart';
import 'package:largefriends/widgets/lf_text_filed.dart';

import 'lf_report_logic.dart';

class LfReportPage extends StatefulWidget {
  const LfReportPage({super.key});

  @override
  State<LfReportPage> createState() => _LfReportPageState();
}

class _LfReportPageState extends State<LfReportPage> {
  final logic = Get.put(LfReportLogic());

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: Scaffold(
          appBar: lfAppBar(
              leadingCallback: () {
                Get.back();
              },
              title: strReport.tr.toUpperCase(),),
          body: Padding(
            padding: EdgeInsets.symmetric(horizontal: 15.w),
            child: ScrollConfiguration(
              behavior: LfScrollBehavior(),
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    LfCategoryWidget(
                      title: strReason.tr,
                      child: Column(
                        children: _reasonWidgets(),
                      ),
                    ),
                    Obx(() {
                      bool onlySelectedOther = false;
                      if (logic.reason.length == 1 &&
                          logic.reason.first == 16) {
                        onlySelectedOther = true;
                      }
                      return LfCategoryWidget(
                        title: onlySelectedOther
                            ? "${strContent.tr}*"
                            : strContent.tr,
                        child: Column(
                          children: [
                            LfTextFiled(
                              textEditingController: logic.controller,
                              isPassword: false,
                              hintText: strReportsAndBlocksTip.tr,
                              maxLine: 8,
                              minLine: 8,
                              focusNode: logic.focusNode,
                              inputFormatters: [
                                LengthLimitingTextInputFormatter(500)
                              ],
                              onChanged: (value) {
                                logic.content.value = value;
                              },
                            ),
                            10.h.sbh,
                            Row(
                              children: [
                                (onlySelectedOther &&
                                        logic.content.value.trim().isEmpty)
                                    ? Text(
                                        strRequired.tr,
                                        style: lfTextStyle(
                                            sp: 14.sp,
                                            fontFamily: fontRegular,
                                            color: color210215),
                                      )
                                    : empty,
                                Expanded(
                                    child: Text(
                                  "${logic.content.trim().length}/500",
                                  textAlign: TextAlign.end,
                                  style: lfTextStyle(
                                      sp: 14.sp,
                                      fontFamily: fontRegular,
                                      color: colorFEFEFE),
                                ))
                              ],
                            )
                          ],
                        ),
                      );
                    }),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Expanded(
                          child: Obx(() => Text(
                                strAlsoBlockThisUser.tr,
                                style: lfTextStyle(
                                    sp: 15.sp,
                                    fontFamily: fontMedium,
                                    color: logic.alsoBlock.isTrue
                                        ? colorFF007F
                                        : color514F62),
                              )),
                        ),
                        8.w.sbw,
                        Obx(() => Switch(
                              value: logic.alsoBlock.isTrue,
                              onChanged: (value) {
                                logic.alsoBlock.value = value;
                              },
                              activeColor: colorFF007F,
                            ))
                      ],
                    ),
                    20.h.sbh,
                    Obx(() => LfButton(
                        enabled: logic.enableReport,
                        text: strReport.tr.toUpperCase(),
                        onPress: () {
                          logic.report();
                        })),
                    20.h.sbh
                  ],
                ),
              ),
            ),
          ),
        ),
        onWillPop: () async {
          Get.back();
          return false;
        });
  }

  List<Widget> _reasonWidgets() {
    return TypeConfig()
        .getTypeConfigListEntity(LfConfigTypeEnum.report)
        .data!
        .map((e) {
      return Obx(() {
        bool isSelected = logic.reason.contains(e.key);
        return InkWell(
          onTap: () {
            if (isSelected) {
              logic.reason.remove(e.key);
            } else {
              logic.reason.add(e.key);
            }
          },
          child: Container(
            padding: EdgeInsets.symmetric(vertical: 6.h),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Expanded(
                  child: Text(
                    e.value,
                    style: lfTextStyle(
                        sp: 14.sp,
                        fontFamily: fontMedium,
                        color: isSelected ? colorFF007F : color514F62),
                  ),
                ),
                10.w.sbw,
                isSelected
                    ? Icon(
                        Icons.check_box_rounded,
                        color: colorFF007F,
                      )
                    : Icon(
                        Icons.check_box_outline_blank,
                        color: color514F62,
                      )
              ],
            ),
          ),
        );
      });
    }).toList();
  }
}
